package com.k2data.k2app.controller;

import com.k2data.k2app.constant.Constant;
import com.k2data.k2app.domain.SysLog;
import com.k2data.k2app.domain.dto.SysLogCondDTO;
import com.k2data.k2app.mapper.SysLogMapper;
import com.k2data.k2app.response.CommonResultResponse;
import com.k2data.k2app.response.PageInfo;
import com.k2data.k2app.response.ResponseHelper;
import com.k2data.k2app.service.SysLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Created by clb on 17-3-17.
 */
@Api(description = "系统日志")
@RestController
@RequestMapping("/logs")
public class SysLogController {
	private final SysLogMapper sysLogMapper;
	@Autowired
	private SysLogService sysLogService;
	@Autowired
	public SysLogController(SysLogMapper sysLogMapper) {
		this.sysLogMapper = sysLogMapper;
	}

	@ApiOperation(value = "创建")
	@ApiImplicitParam(name = "sysLog", value = "sysLog对象", required = true, dataType = "SysLog", paramType = "body")
	@PostMapping(value = "", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<CommonResultResponse> insert(@RequestBody SysLog sysLog) throws Exception {
		return ResponseHelper.success(sysLogService.insertSelected(sysLog));
	}

	@ApiOperation(value = "根据id获取")
	@ApiImplicitParam(name = "id", value = "the value of id", required = true, dataType = "String", paramType = "path")
	@GetMapping(value = "/{id}")
	public ResponseEntity<CommonResultResponse> get(@PathVariable("id") String id) throws Exception {
		return ResponseHelper.success(sysLogMapper.get(Long.parseLong(id)));
	}

	@ApiOperation(value = "获取所有记录")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小", dataType = "int", paramType = "query"),
			@ApiImplicitParam(name = "keyWord", value = "关键字", dataType = "string", paramType = "query"),
			@ApiImplicitParam(name = "startDateTime", value = "开始时间", dataType = "date", paramType = "query"),
			@ApiImplicitParam(name = "endDateTime", value = "结束时间", dataType = "date", paramType = "query")
	})
	@GetMapping(value = "")
	public ResponseEntity<CommonResultResponse> selectAll(
			@RequestParam(value = "pageNum", required = false) Integer pageNum,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			@RequestParam(value = "keyWord", required = false) String keyWord,
			@RequestParam(value = "startDateTime", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startDateTime,
			@RequestParam(value = "endDateTime", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endDateTime) throws Exception {
		int pageNumReal = pageNum == null ? 1 : pageNum;
		int pageSizeReal = pageSize == null ? Constant.DEFAULT_PAGE_SIZE : pageSize;
		SysLogCondDTO sysLogCondDTO = new SysLogCondDTO();
		sysLogCondDTO.setKeyWord(keyWord);
		sysLogCondDTO.setStartDateTime(startDateTime);
		sysLogCondDTO.setEndDateTime(endDateTime);
		List<SysLog> sysLogs = sysLogService.selectSysLog(sysLogCondDTO);
		int count = sysLogs.size();
		PageInfo pageInfo = new PageInfo(pageNumReal, pageSizeReal, count);
		int fromIndex = (pageNumReal - 1) * pageSizeReal;
		if (fromIndex > count) {
			fromIndex = count;
		}
		int toIndex = pageNumReal * pageSizeReal;
		if (toIndex > count) {
			toIndex = count;
		}
		return ResponseHelper.success(sysLogs.subList(fromIndex, toIndex), pageInfo);
	}
}
